package com.glamour.hlcity.dal.lock.dao.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.glamour.hlcity.dal.lock.dao.Lock;

public class DBLock extends SqlMapClientDaoSupport implements Lock {
	
	private final static String GET_LOCK_BY_CODE="LockPO.getLockByCode";
	private final static String RELEASE_LOCK_BY_CODE="LockPO.releaseLockByCode";
	
	@Override
	public Integer getLock(String lockName,Integer time) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("lockName", lockName);
		map.put("time", time);
		return (Integer) this.getSqlMapClientTemplate().queryForObject(GET_LOCK_BY_CODE, map);
	}
	
	@Override
	public Integer releaseLock(String lockName) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(RELEASE_LOCK_BY_CODE, lockName);
	}

	@Override
	public Integer getLockNoWait(String lockName) {
		return getLock(lockName, BigDecimal.ZERO.intValue());
	}
}
